﻿using NullSoftware.ToolKit;
using System.Drawing;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using XYAC.Framework.WatchDog;


namespace XYAC.Framework.MonitorTireMaterialManual
{
    public class TrayIconManager : IDisposable
    {
        public Icon NormalIcon { get; }
        public Icon SuccessIcon { get; }
        public Icon FailedIcon { get; }
        public TrayIcon TrayIcon { get; private set; }

        public TrayIconManager()
        {

            InitializeNotifyIcon();
        }

        private void InitializeNotifyIcon()
        {
            var icon = BitmapFrame.Create(new Uri("pack://application:,,,/XYAC.Framework.MonitorTireMaterialManual;component/Assets/main.ico"));

            TrayIcon = new TrayIcon()
            {
                Title = "C区离线检测数据同步",
                IconSource = icon,

            };

            TrayIcon.Click += TrayIcon_Click;
            var exitMenu = new MenuItem() { Header = "退出" };
            exitMenu.Click += ExitMenu_Click;
            TrayIcon.ContextMenu = new System.Windows.Controls.ContextMenu();
            TrayIcon.ContextMenu.Items.Add(exitMenu);
        }

        private void ExitMenu_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("确定退出本应用吗?", "提示", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
            {
                WatchDogStarter.Exit();
                Application.Current.Shutdown();
                this.Dispose();//销毁图标缓存
            }
        }

        private void TrayIcon_Click(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            // 恢复主窗口
            if (Application.Current.MainWindow != null && e.ChangedButton == System.Windows.Input.MouseButton.Left)
            {
                if (Application.Current.MainWindow.IsVisible)
                {

                    Application.Current.MainWindow.Hide();
                }
                else
                {
                    Application.Current.MainWindow.Show();
                    Application.Current.MainWindow.WindowState = WindowState.Maximized;
                }
            }

        }






        public void Dispose()
        {
            this.TrayIcon.Dispose();
        }
    }
}
